﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using urBook.Engine;
namespace urBook.Gui
{
	/// <summary>
	/// Interaction logic for ElementView.xaml
	/// </summary>
	public partial class ElementView : UserControl
	{
        ContactCard contact;
        public Item item { get; set; }
        string className;

		public ElementView(ContactCard contact, Item item, string className)
		{
			this.InitializeComponent();
            this.item = item;
            this.type.SelectedItem = getSelectedItem(this.type.Items,item.type.ToString());
            this.value.Text = item.value;
            this.value.ToolTip = className;
            this.className = className;
            this.contact = contact;
            this.isPreffered.IsChecked = item.isPreferred;
		}

        private object getSelectedItem(ItemCollection items, string type)
        {
            foreach (ComboBoxItem cbi in items)
                if (cbi.Content.ToString().ToLower().Equals(type.ToLower()))
                    return cbi;
            return null;
        }

        private Engine.Type getSelectedType(string type)
        {
            Engine.Type result;

            switch (type)
            {
                case "Home": result = Engine.Type.HOME; break;
                case "Work": result = Engine.Type.WORK; break;
                case "Cell": result = Engine.Type.CELL; break;
                case "Fax": result = Engine.Type.FAX; break;
                case "Internet": result = Engine.Type.INTERNET; break;
                default: result = Engine.Type.OTHER; break;
            }

            return result;
        }

        private void deleteElement_MouseDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                ListBox parent = (ListBox)Parent;
                parent.Items.Remove(this);
                if (className.Equals("Telephone"))
                    contact.phones.Remove((Telephone)item);
                else if (className.Equals("Email"))
                    contact.emails.Remove((Email)item);
            }
            catch (Exception a) { }
        }

        private void isPreffered_Click(object sender, RoutedEventArgs e)
        {
            item.isPreferred = (bool)isPreffered.IsChecked;
        }

        private void value_TextChanged(object sender, TextChangedEventArgs e)
        {
            item.value = value.Text.Trim().ToLower();
        }

        private void type_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBoxItem selected = (ComboBoxItem)type.SelectedItem;
            item.type = getSelectedType(selected.Content.ToString());
        }
	}
}